package com.jie.controller;


import com.jie.dao.OrdersDao;
import com.jie.entity.Goods;
import com.jie.entity.OrderInfo;
import com.jie.entity.Orders;
import com.jie.service.GoodsService;
import com.jie.service.OrdersService;
import com.jie.utility.R;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jie
 * @since 2024-03-21
 */

@RestController
@RequestMapping("/orders")
@Transactional
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RestHighLevelClient client;

    /**
     * 添加订单
     * @param orders
     * @return
     */
    @PostMapping("/addOrders")
    public R<String> addOrders(@RequestBody Orders orders){
        if(StringUtils.isNotEmpty(orders.getOrdersId())){
            Orders byId = ordersService.getById(orders.getOrdersId());
            if(byId!=null){
                byId.setOrdersId("");
                byId.setCreateTime(null);
                Goods goods = goodsService.getById(byId.getGoodsId());
              if(goods.getGoodsNumber()>byId.getNumber()){
                Double q=goods.getQuantitySold()+byId.getNumber();
                double num=goods.getGoodsNumber()-byId.getNumber();
                String status="1";
                goods.setQuantitySold(q);
                if(num<1){
                    num=0;
                    status="0";
                    goods.setGoodsState("0");
                    goods.setGoodsNumber(0);
                }else {
                    goods.setGoodsNumber((int) num);
                }
                boolean b = goodsService.updateById(goods);
                if(b){
                    UpdateRequest request = new UpdateRequest("goods", goods.getGoodsId());
                    // 使用 XContentBuilder 构建要更新的数据
                    XContentBuilder builder = null;
                    try {
                        builder = XContentFactory.jsonBuilder();
                        builder.startObject()
                                .field("goodsNumber", num)
                                .field("goodsState", status)
                                .field("quantitySold", q);
                        builder.endObject();
                        request.doc(builder);
                        client.update(request, RequestOptions.DEFAULT);
                        boolean save = ordersService.save(byId);
                        if(save){
                            return R.success("购买成功！");
                        }else {
                            return R.success("购买失败！");
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
               }
              else {
                  return R.success("该订单没有库存了！");
              }
            }else {
                return R.success("该订单删除了！");
            }
        }
        Goods goods = goodsService.getById(orders.getGoodsId());
        if(goods.getGoodsNumber()>orders.getNumber()){
            boolean save = ordersService.save(orders);
        if(save){
            Double q=goods.getQuantitySold()+orders.getNumber();
            double num=goods.getGoodsNumber()-orders.getNumber();
            String status="1";
            goods.setQuantitySold(q);
            if(num<1){
                num=0;
                status="0";
                goods.setGoodsState("0");
                goods.setGoodsNumber(0);
            }else {
                goods.setGoodsNumber((int) num);
            }
            boolean b = goodsService.updateById(goods);
            if(b){
                UpdateRequest request = new UpdateRequest("goods", goods.getGoodsId());
                // 使用 XContentBuilder 构建要更新的数据
                XContentBuilder builder = null;
                try {
                    builder = XContentFactory.jsonBuilder();
                    builder.startObject()
                            .field("goodsNumber", num)
                            .field("goodsState", status)
                            .field("quantitySold", q);
                   builder.endObject();
                    request.doc(builder);
                    client.update(request, RequestOptions.DEFAULT);
                    return R.success("购买成功！");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return R.success("购买失败！");
        }
        }
        return R.success("商品库存没有了！");
    }
    /**
     * 前端获取订单信息
     */
    @GetMapping("/allOrders")
    public R<List<OrdersDao>> allOrders(Orders orders){
        List<OrdersDao> ordersDaos=ordersService.allOrders(orders);
        return R.success(ordersDaos);
    }

    /**
     * 后端获取订单数据
     * @param orderInfo 获取条件
     * @return
     */
    @PostMapping("/getOrders")
    public  R<List<OrdersDao>> getOrders(@RequestBody OrderInfo orderInfo){
        return ordersService.getOrders(orderInfo);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping("/editOrders")
    public R<String>editOrders(@RequestBody Orders orders){
        boolean b = ordersService.updateById(orders);
        if(b)return R.success("");
        return R.error("失败！");
    }



}

